package com.hoongs.v1.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hoongs.v1.dto.SettingsPageDto;
import com.hoongs.v1.entity.Settings;
import com.hoongs.v1.service.SettingsService;
import com.hoongs.v1.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/settings")
@Validated
public class SettingsController {

    @Resource
    private SettingsService settingsService;

    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result add(@Validated Settings settings, BindingResult bindingResult) {
        String res = Result.resultMsg(bindingResult);
        return StringUtils.isNotBlank(res) ? Result.opError(res) : settingsService.add(settings);
    }

    @PostMapping("/remove")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result remove(String id) {
        return settingsService.remove(id);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result update(Settings settings) {
        return settingsService.update(settings);
    }


    @GetMapping("/selectById")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result selectById(String id) {
        return settingsService.selectById(id);
    }

    @GetMapping("/selectByProp")
    public Result selectByProp(String prop) {
        return settingsService.selectByProp(prop);
    }

    @GetMapping("/selectList")
    public Result selectList(String name,  String prop) {
        return settingsService.selectList(name,  prop);
    }

    @GetMapping("/selectPageList")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result selectPageList(SettingsPageDto settingsDto) {
        return settingsService.selectPageList(settingsDto);
    }

}
